//
//  HouseCarLoanHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/3.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol HouseCarLoanHeadViewDelegate {
    @objc optional func selectCityType() //选择城市
}

class HouseCarLoanHeadView: UIView {
    
    weak var delegate : HouseCarLoanHeadViewDelegate?
    
    let bankButton = ButtonView()
    fileprivate let titleLabel = UILabel()
    fileprivate let locationBtn = UIButton()
    fileprivate let cityButton = UIButton()
    
    var number : String!{
        didSet{
            titleLabel.text = "为您找到 " + number + " 个产品"
            titleLabel.attributedText = titleLabel.text?.setTextColor(color: UIColor.red, ranStr: number)
        }
    }
    
    var bankName : String! {
        didSet{
            bankButton.text = bankName
        }
    }
    
    var cityName : String!{
        didSet{
            cityButton.setTitle(cityName, for: .normal)
            
            cityButton.snp.remakeConstraints { (make) in
                make.right.equalToSuperview().offset(RATIO(maxNum: -10))
                make.centerY.equalToSuperview()
                make.width.equalTo(cityName.size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 30)).width-8)
                make.height.equalTo(RATIO_H(maxNum: 30))
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bankButton.image = UIImage.init(named: "bottom_arrow")
        bankButton.text = everyBanks
        bankButton.layer.borderWidth = 1
        bankButton.layer.borderColor = UIColor.lineColor().cgColor
        self.addSubview(bankButton)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleLabel.textAlignment = .center
        self.addSubview(titleLabel)
        
        locationBtn.layer.contents = UIImage.init(named: "bank_location")?.cgImage
        locationBtn.addTarget(self, action: #selector(selectCityType), for: .touchUpInside)
        self.addSubview(locationBtn)
        
        cityButton.setTitleColor(UIColor.red, for: .normal)
        cityButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        cityButton.addTarget(self, action: #selector(selectCityType), for: .touchUpInside)
        self.addSubview(cityButton)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.addSubview(lineView)
        
        bankButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: RATIO(maxNum: 10)))
            make.width.equalTo(RATIO(maxNum: 120))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        cityButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.centerY.equalToSuperview()
            make.width.equalTo(RATIO(maxNum: 40))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        locationBtn.snp.makeConstraints { (make) in
            make.right.equalTo(cityButton.snp.left)
            make.centerY.equalTo(cityButton)
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bankButton.snp.right)
            make.right.equalTo(locationBtn.snp.left)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview()
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    //MARK: 选择城市
    @objc fileprivate func selectCityType() {
    
        self.delegate?.selectCityType!()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
